Java continue statement
๐ Java continue
Statement: The Art of Skipping! ๐ญโ
The continue
statement in Java is like that one friend who skips all the boring parts of a conversation and jumps straight to the good stuff. ๐ It helps us skip the current iteration of a loop and move on to the next oneโno hard feelings!
๐ฏ What's the Deal with continue
?โ
The continue
statement works inside for, while, or do-while loops and behaves almost like break
, except it doesnโt rudely terminate the loopโit just skips the current iteration and moves to the next.
๐ค How Does It Work?โ
- In a for loop,
continue
jumps directly to the increment/decrement part. - In a while or do-while loop,
continue
sends control straight to the condition check.
Letโs see continue
in action:
int n = 10;
for (int i = 0; i < n; i++) {
if (i % 2 != 0) { // Skip odd numbers
continue;
}
System.out.print(i + " ");
}
๐น Output:
0 2 4 6 8
See? All the odd numbers just vanished! Magic? Nope. Just continue
doing its thing. ๐ช
๐ฅ Syntax of continue
โ
The syntax is as simple as skipping leg day at the gym. ๐ช
while (testExpression) {
// Some statements
if (continue-condition)
continue;
// More statements (skipped if continue is hit)
}
When the continue-condition
is met, Java skips the remaining code inside the loop and jumps straight to the next iteration.
๐ญ Types of continue
Statementsโ
Java offers two flavors of continue
statements:
๐ฉ 1. Unlabeled continue
โ
This is the basic form of continue
. It simply skips the current iteration of the loop itโs in.
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number: " + i);
}
๐น Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
๐ฏ 2. Labeled continue
โ
Sometimes, you need more control. Labeled continue
lets you skip an iteration in an outer loop instead of just the current one. Think of it as a VIP pass to the next level. ๐๏ธ
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer; // Skip to the next iteration of outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}
๐น Output:
i = 0, j = 0
i = 1, j = 0
i = 2, j = 0
Whenever j == 1
, it skips the rest of the inner loop and moves to the next iteration of the outer loop. Pretty neat, right? ๐
๐ฌ continue
in Action: A Real-Life Exampleโ
Letโs use continue
to print only odd numbers from 0 to 9:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("The number is " + i);
}
๐น Output:
The number is 1
The number is 3
The number is 5
The number is 7
The number is 9
Now, what if we donโt use continue
? We can achieve the same result with an if
statement:
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) {
System.out.println("The number is " + i);
}
}
The result is the same, but heyโsometimes continue
just makes things more fun! ๐ข
โ ๏ธ A Word of Cautionโ
While continue
is super useful, donโt overuse it! Too many continue
and break
statements can make your code hard to read. Sometimes, a simple if
statement does the job just as well.
๐ Conclusionโ
The continue
statement is like a "skip this one" button for loops. Itโs useful when you need to avoid certain iterations without stopping the entire loop. ๐
- Use unlabeled
continue
to skip the current iteration. - Use labeled
continue
when working with nested loops. - Donโt abuse itโclean code is happy code! ๐
Thatโs all, folks! Keep coding and keep skipping the boring parts! ๐
Happy Learning! ๐๐